
package cn.gzsxt.music.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gzsxt.music.query.CDQueryObject;
import cn.gzsxt.music.service.CDService;
import cn.gzsxt.music.vo.MsgObj;

@Controller
@Scope("request")
@RequestMapping("/cd")
public class CDController {
	
	@Autowired
	private CDService cdService;
	
	/**
	 * 点击左侧专辑管理菜单跳转到页面
	 * @return 返回跳转的页面路径
	 */
	@RequestMapping("/cdPage")
	public String cdPage() {
		return "cdPage";
	}
	
	/**
	 * 查询出所有的专辑信息并分页显示到页面上
	 * @param cqo 分页显示的信息
	 * @return
	 */
	@RequestMapping("/cdList")
	@ResponseBody
	public Map<String, Object> cdList(CDQueryObject cqo){
		Map<String, Object> cdMap = cdService.cdList(cqo);
		System.out.println(cqo.toString());
		return cdMap;
	}
	
	/**
	 * 根据用户输入的专辑名查询,判断专辑是否已存在
	 * @param request 地址栏信息
	 * @return 返回专辑数量的消息
	 * @throws UnsupportedEncodingException 使用URLDecoder类的编码方法可能会出现  不支持的字符集异常
	 */
	/*@RequestMapping("/cdSelect")
	@ResponseBody
	public MsgObj cdSelect(HttpServletRequest request) throws UnsupportedEncodingException{
		//将前台转码的地址栏参数先转会utf-8格式,再对数据进行操作
		String url = URLDecoder.decode(request.getParameter("cd_name"), "utf-8");
		
		Map<String, Object> map = new HashMap<>();
		map.put("cd_name", url);
		
		Map<String, Object> cdSelect = cdService.cdSelect(map);
		
		MsgObj msgObj = new MsgObj(1, "专辑已存在");
		if(cdSelect == null) {
			msgObj = new MsgObj(0, "专辑不存在");
		}
		return msgObj;
	}*/
	
	//查找所有的歌手显示在添加/修改对话框中
	@RequestMapping("/selectSinger")
	@ResponseBody
	public List<Map<String, Object>> selectSinger() {
		List<Map<String, Object>> singerList = cdService.selectSinger();
		return singerList;
	}
	
	//添加一个新的专辑
	@RequestMapping("/addCD")
	@ResponseBody
	public MsgObj addCD(@RequestParam Map<String, Object> map) {
		int row = cdService.addCD(map);
		MsgObj msgObj = new MsgObj(1, "添加成功");
		if(row == 0) {
			msgObj = new MsgObj(0, "添加失败");
		}
		return msgObj;
	}
	
	//修改专辑
	@RequestMapping("/updateCD")
	@ResponseBody
	public MsgObj updateCD(@RequestParam Map<String, Object> map) {
		int row = cdService.updateCD(map);
		MsgObj msgObj = new MsgObj(1, "修改成功");
		if(row == 0) {
			msgObj = new MsgObj(0, "修改失败");
		}
		return msgObj;
	}
	
	//删除专辑
	@RequestMapping("/deleteCD")
	@ResponseBody
	public MsgObj deleteCD(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		map.put("cd_id", request.getParameter("cd_id"));
		int row = cdService.deleteCD(map);
		MsgObj msgObj = new MsgObj(1, "删除成功");
		if(row == 0) {
			msgObj = new MsgObj(0, "删除失败");
		}
		return msgObj;
	}
}
